      <div class="templatemo-content-wrapper">
        <div class="templatemo-content">
          <ul class="nav nav-tabs mytabs">
		  	 <li  class="active"><a href="<?php echo site_url('product/materialindex/id/'.$product['id'])?>">配方列表</a></li>
		  	 <li><a href="<?=site_url()?>/product/index">商品列表</a></li>
		  </ul>
		  <div class="row margin-bottom-15">
            <div class="col-md-12">
				<input type="text" id="name" name="name" value="" placeholder="请输入配方">
                <input type="text" id="amount" name="amount" value="" placeholder="请输入数量(单位：克)">
                <input type="hidden" id="material" name="material" value="">
                <input type="hidden" id="product" name="product" value="<?php echo $product['id'];?>">
                <button class="btn btn-primary" id="submit_btn">添加</button>
            </div>
          </div>
          <div class="row">
            <div class="col-md-12">
              <div class="table-responsive">
              	<h4 class="margin-bottom-15"><?php echo $product['name'];?></h4>
                <table class="table table-striped table-hover table-bordered">
                  <thead>
                    <tr>
                      <th>ID</th>
                      <th>配方</th>
                      <th>数量</th>
                      <th>操作</th>
                    </tr>
                  </thead>
                  <tbody>
                  	<?php foreach ($materials as $item):?>
                    <tr>
                      <td><?php echo $item['materialid'];?></td>
                      <td><?php echo $item['name'];?></td>
                      <td><?php echo $item['amount'].'克';?></td>
                      <td>
                      	    <a href="#" class="del" data-id="<?php echo $item['id'];?>" >删除</a>
                      </td>
                    </tr>
                    <?php endforeach;?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>
<script>
	$(document).ready(function() {
		$("#name").autocomplete({
			minLength:1,
			source:function(request,response){
       			 $.ajax({
                    url:'<?php echo site_url('nutrition/getNutritions')?>',
                    type : "post",
                    dataType: "json",
                    data:{
                        name: request.term
                    },
                    success: function( data ) {
                        response($.map(data, function(item) {
    		            return {
    		                 label: item.name,
    		                 value: item.name,
    		                 id:item.id
    		             }
    		         }));
                    }
                });
			},
			select: function( event, ui ) {
        		$( "#material" ).val( ui.item.id );
      		}
		});
		$("#submit_btn").click(function() {
			var product = $("#product").val();
			var material = $("#material").val();
			var amount = $("#amount").val();
			$.ajax({
				url : '<?php echo site_url('product/addmaterial')?>',
				type : "post",
				async : false,
				data : {
					'product':product,
					'material':material,
					'amount':amount
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
		});


		$(".del").click(function() {
			if(confirm("确定要删除该配方吗？"))
			{
				var id = $(this).attr("data-id");
				$.ajax({
					url : '<?php echo site_url('product/deletematerial')?>',
					type : "post",
					async : false,
					data : {
						'id' : id
					},
					dataType : "json",
					success : function(data) {
						if(data.code)
						{
							alert(data.msg);
							window.location.reload();
						}else{
							alert(data.msg);
						}
					}
				});
			}
		});
	});
</script>
